<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | 工序数据控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\exception\LogicException;
use common\service\TaskWorkOrderService;
use think\Exception;
use think\Request;
use think\Response;
use Throwable;

class TaskWorkOrderController extends VerifyController
{
    private TaskWorkOrderService                     $taskWorkOrderService;

    public function __construct()
    {
        parent::__construct();
        $this->taskWorkOrderService = TaskWorkOrderService::getInstance();
    }

    /**
     * 创建工单
     * @param Request $request
     * @return Response
     * @throws \Exception|Throwable
     */
    public function createTaskWorkOrder(Request $request)
    {
        try {
            $res = $this->taskWorkOrderService->createTaskWorkOrder($request->param()['data'], $request->getTenantId(), $request->getUserId(), $request->getUnionUserId(), false);
            return json(success_response('success', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 查询资产任务工单查询
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function selectAssetTaskWorkOrderSelect(Request $request)
    {
        $param = $request->param()['param'];
        $res = $this->taskWorkOrderService->selectAssetTaskWorkOrderSelect($param, $request->getUserId(), $request->getTenantId());
        return json(success_response('success', $res));
    }


    /**
     * 获取单个任务工单数据，包含甲方和乙方
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function getTaskWorkOrderRecordById(Request $request)
    {
        $param = $request->param()['param'];
        $filter = $param['filter'] ?? [];
        $page = $param["page"] ?? [1, C('default_page_total')];

        $res = $this->taskWorkOrderService->getTaskALLWorkOrderRecords($filter, $page);
        return json(success_response('success', $res));
    }

    /**
     * 工单记录树
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function taskWorkOrderRecordTree(Request $request)
    {
        $param = $request->param()['param'];
        $filter = $param['filter'] ?? [];
        $page = $param["page"] ?? [1, C('default_page_total')];

        $res = $this->taskWorkOrderService->taskWorkOrderRecordTree($filter, $page);
        return json(success_response('success', $res));
    }

    /**
     * 获取工单详情
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function taskWorkOrderRecordDetails(Request $request)
    {
        $param = $request->param()['param'];
        $res = $this->taskWorkOrderService->taskWorkOrderRecordDetails($param, $request->getTenantId());
        return json(success_response('success', $res));
    }

    /**
     * 回复工单
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function replyTaskWorkOrder(Request $request)
    {
        $data = $request->param()['data'];
        try {
            $res = $this->taskWorkOrderService->replyTaskWorkOrder($data, $request->getTenantId(), $request->getUserId());
            return json(success_response('success', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 处理反馈工单
     * @param Request $request
     * @return Response
     * @throws Throwable
     * @throws Exception
     */
    public function dealReviewFeedbackTaskWorkOrder(Request $request)
    {
        try {
            $data = $request->param()['data'];
            $res = $this->taskWorkOrderService->dealReviewFeedbackTaskWorkOrder($data);
            return json(success_response('success', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 制作管理
     * @param Request $request
     * @return Response
     */
    public function processManage(Request $request)
    {
        $param = $request->param()['param'];
        try {
            $res = $this->taskWorkOrderService->processManage($param, $request->getTenantId());
            return json(success_response('success', $res));
        } catch (Exception $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 更新工单
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function updateTaskWorkOrder(Request $request)
    {
        $data = $request->param()['data'];
        $userId = $request->getUserId();
        try {
            $res = $this->taskWorkOrderService->updateTaskWorkOrder($data, $userId);
            return json(success_response('success', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 获取工单详细信息
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function taskWorkOrderInformation(Request $request)
    {
        $data = $request->param();
        $taskWorkOrderId = $data['param']['filter']['task_work_order_id'];
        $res = $this->taskWorkOrderService->taskWorkOrderInformation($taskWorkOrderId, $request->getTenantId(), $request->userId);
        return json(success_response('success', $res));
    }

    /**
     * 清出掉未读消息
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function clearTaskWorkUnread(Request $request)
    {
        $data = $request->param()['data'];
        $res = $this->taskWorkOrderService->clearTaskWorkUnread($data, $request->getUserId());
        return json(success_response('success', $res));
    }

    /**
     * 获取工单会话
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function getTaskWorkOrderChat(Request $request)
    {
        $param = $request->param()['param'];
        $res = $this->taskWorkOrderService->getTaskWorkOrderChat($param, $request->getUserId());
        return json(success_response('success', $res));
    }

    /**
     * 获取未读工单会话
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function getUnreadTaskWorkOrderChat(Request $request)
    {
        $param = $request->param()['param'];
        $res = $this->taskWorkOrderService->getUnreadTaskWorkOrderChat($param, $request->getUserId());
        return json(success_response('success', $res));
    }

    /**
     * 获取单条工单会话
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function findTaskWorkOrderChat(Request $request)
    {
        $taskWorkOrderId = $request->param()['param']['filter']['id'];
        $res = $this->taskWorkOrderService->findTaskWorkOrderChat($taskWorkOrderId);
        return json(success_response('success', $res));
    }

    /**
     * 获取工单下的返修工单
     * @param Request $request
     * @return Response
     */
    public function getWorkOrderRepairWorkOrder(Request $request)
    {
        $param = $request->param()['param'];
        $res = $this->taskWorkOrderService->getWorkOrderRepairWorkOrder($param);
        return json(success_response('success', $res));
    }

    /**
     * 获取返修工单受影响的任务
     * @param Request $request
     * @return Response
     */
    public function getRepairOrderAffectedTask(Request $request)
    {
        $param = $request->param()['param'];
        $res = $this->taskWorkOrderService->getRepairOrderAffectedTask($param);
        return json(success_response('success', $res));
    }

    /**
     * 获取实体工序的返修工单
     * @param Request $request
     * @return Response
     */
    public function getEntityRepairWorkOrder(Request $request)
    {
        $param = $request->param()['param'];
        $res = $this->taskWorkOrderService->getEntityRepairWorkOrder($param);
        return json(success_response('success', $res));
    }

    /**
     * 加入工单
     * @param Request $request
     * @return Response
     * @throws Throwable
     */
    public function join(Request $request)
    {
        try {
            $taskWorkOrderId = $request->param()['param']['task_work_order_id'];
            $res = $this->taskWorkOrderService->join($taskWorkOrderId, $request->getUserId());
            return json(success_response('success', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 工单列表
     * @param Request $request
     * @return Response|void
     */
    public function list(Request $request)
    {
        try {
            $param = $request->param()['param'];
            $res = $this->taskWorkOrderService->list($param, $request->getUserId(), $request->getTenantId());
            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 工作待办-我的工单列表
     *
     * @return void|array
     */
    public function getMyTodoTaskWorkOrderList(Request $request)
    {
        try {
            $param = $request->param()['param'];
            $res   = $this->taskWorkOrderService->getWorkToDoMyTaskWorkOrderList(
                $param, $request->getUserId(), $request->getTenantId()
            );

            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 工作待办-工单统计
     * @param \think\Request $request
     *
     * @return \think\Response
     */
    public function getMyTodoTaskWorkOrderCount(Request $request)
    {
        try {
            $param = $request->param()['param'];
            $res   = $this->taskWorkOrderService->getMyTodoTaskWorkOrderCount(
                $param, $request->getUserId(), $request->getTenantId()
            );

            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

	/**
	 * 工作待办-左侧工单数据统计
	 * @param \think\Request $request
	 *
	 * @return \think\Response
	 */
	public function getTodoTaskStatisticsOnTheLeft(Request $request)
	{
		try {
			$param = $request->param()['param'];
			$res   = $this->taskWorkOrderService->getTodoTaskStatisticsOnTheLeft(
				$param, $request->getUserId(), $request->getTenantId()
			);

			return json(success_response('OK', $res));
		} catch (LogicException $e) {
			throw_strack_exception($e->getMessage(), $e->getCode());
		}
	}

    /**
     * 工单上报类型统计
     * @param Request $request
     * @return Response|void
     */
    public function typeCount(Request $request)
    {
        try {
            $param = $request->param()['param'];
            $res = $this->taskWorkOrderService->typeCount($param, $request->getUserId(), $request->getTenantId());
            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 报表
     * @param Request $request
     * @return Response|void
     */
    public function report(Request $request)
    {
        try {
            $param = $request->param()['param'];
            $reportType = $param['report_type'];
            $stepCategoryType = $param['step_category_type'];
            $res = $this->taskWorkOrderService->report($param, $reportType, $stepCategoryType, $request->getTenantId());
            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 上报并添加反馈
     * @param Request $request
     * @return Response|void
     * @throws Throwable
     */
    public function reportAndAddFeedback(Request $request)
    {
        try {
            $data = $request->param()['data'];
            $startRepairTaskId = $data['start_repair_task_id'];
            $wantRepairTaskId = $data['want_repair_task_id'];
            $reportType = $data['report_type'];
            $workOrderId = $data['work_order_id'] ?? 0;
            $res = $this->taskWorkOrderService->reportAndAddFeedback(
                $data, $reportType, $startRepairTaskId, $wantRepairTaskId, $workOrderId, $request->getUserId(),
                $request->getUnionUserId(), $request->getTenantId()
            );
            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 工单返修反馈实体列表
     * @param Request $request
     * @return Response|void
     */
    public function repairNoteEntityList(Request $request)
    {
        try {
            $param = $request->param()['param'];
            $page = $param["page"] ?? [1, C('default_page_total')];
            $res = $this->taskWorkOrderService->repairNoteEntityList($param, $page);
            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 删除
     * @param Request $request
     * @return Response|void
     */
    public function delete(Request $request)
    {
        try {
            $filter = $request->param()['param']['filter'];
            $res = $this->taskWorkOrderService->delete($filter);
            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 获取问题环节详情列表
     * @param \think\Request $request
     * @return \think\Response
     */
    public function getProblemStepCategoryInfoList(Request $request)
    {
        $param = $request->param()['param'];
        $res = $this->taskWorkOrderService->getProblemStepCategoryInfoList($param, $request->getUserId());
        return json(success_response('OK', $res));
    }

    /**
     * 是否存在工单默认配置人员
     * @param \think\Request $request
     * @return \think\Response
     */
    public function isExistTaskWorkOrderDefaultOptionMember(Request $request)
    {
        $filter = $request->param()['param']['filter'];
        $res = $this->taskWorkOrderService->isExistTaskWorkOrderDefaultOptionMember($filter['problem_step_category_ids'], $filter['project_id']);
        return json(success_response('OK', $res));
    }

}
